﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace BarrierGenarator.Net
{
    class HttpRequest
    {
        public static string url = "http://45.251.242.42:8080/";
        public static string Upload_BarrierJson(string name ,string author,int difficulty,string json)
        {
            string strURL = string.Format("{0}?snake={1}", url,"upload");
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream(),Encoding.Unicode))
            {
                streamWriter.Write(name + "%" + author + "%" + difficulty.ToString() + "%" + json);
                streamWriter.Flush();
                streamWriter.Close();
            }
            var httpresponse = (HttpWebResponse)request.GetResponse();
            using (var streamreader = new StreamReader(httpresponse.GetResponseStream()))
            {
                var result = streamreader.ReadToEnd();
                return result;
            }
        }
        public static string Login(string username, string password)
        {
            string strURL = string.Format("{0}?snake={1}", url, "login");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(username + "%" + password);
                writer.Flush();
                writer.Close();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                reader.Close();
                return result;
            }
        }
    }
}
